package com.yale.cxfsoapws.service.impl;

import com.yale.cxfsoapws.model.User;
import com.yale.cxfsoapws.service.UserService;

import javax.jws.WebService;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Service implementation Bean（SIB）
 * RPC接口实现
 * 注意这里的targetNamespace的命名空间为SEI对应的命名空间，注意最后要加/
 * 否则利用CXF动态客户端调用时，会找不到
 * Exception in thread "main" org.apache.cxf.common.i18n.UncheckedException:
 * No operation was found with the name {http://impl.service.jws/}sum.
 * @author yale
 * @date 2017/8/16
 * @version 2.0
 *
 * 业务代码
 *
 */
@WebService(endpointInterface = "com.yale.cxfsoapws.service.UserService",
    serviceName = "userService",
    portName = "userPort",
    targetNamespace = "/service.jws/"
    // targetNameSpace = "/www.donald.service/jws_service/"
)
public class UserServiceImpl implements UserService {

    private Map<Long, User> userMap = new HashMap<Long, User>();

    public UserServiceImpl(){
        User user = new User();
        user.setUserId(10001L);
        user.setUsername("yale1@qq.com");
        user.setGmtCreate(new Date());
        userMap.put(user.getUserId(), user);

        user = new User();
        user.setUserId(10002L);
        user.setUsername("yale2@qq.com");
        user.setGmtCreate(new Date());
        userMap.put(user.getUserId(), user);

        user = new User();
        user.setUserId(10003L);
        user.setUsername("yale3@qq.com");
        user.setGmtCreate(new Date());
        userMap.put(user.getUserId(), user);
    }

    @Override
    public String getName(Long userId) {
        System.out.println("userService 1");
        return "yale_" + userId;
    }

    @Override
    public User getUser(Long userId) {
        System.out.println("userService 2");
        return userMap.get(userId);
    }
}
